﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01复习
{
   public static class SqliteHelper
    {
        //从配置文件读取连接字符串
        private static string connStr = ConfigurationManager.ConnectionStrings["itcastCater"].ConnectionString;

        //执行命令的方法：insert,update,delete
        //params:可变参数，目的是省略了手动构造数组的过程，直接指定对象，编译器会帮助我们构造数组，并将对象加入数组中，传递过来
        public static int ExecuteNonQuery(string sql,params SQLiteParameter[] ps)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                SQLiteCommand cmd = new SQLiteCommand(sql, conn);
                cmd.Parameters.AddRange(ps);

                conn.Open();
                return cmd.ExecuteNonQuery();
            }
        }

        //返回首行首列
        public static object ExecuteScalar(string sql,params SQLiteParameter[] ps )
        {
            using (SQLiteConnection conn=new SQLiteConnection(connStr))
            {
                SQLiteCommand cmd = new SQLiteCommand(sql, conn);
                cmd.Parameters.AddRange(ps);

                conn.Open();
                return cmd.ExecuteScalar();
            }
        }
        //获取结果集
        public static DataTable GetDataTable(string sql, params SQLiteParameter[] ps)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn);

                DataTable dt = new DataTable();

                adapter.SelectCommand.Parameters.AddRange(ps);

                adapter.Fill(dt);

                return dt;
            }
        }




    }
}
